package com.dinzeer.slashblade_useful_addon.events;

// ... existing imports ...

import com.dinzeer.slashblade_useful_addon.config.Config;
import com.dinzeer.slashblade_useful_addon.SUAModKeyMappings;
import mods.flammpfeil.slashblade.entity.BladeStandEntity;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class DoInteract {
    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public static void onPlayerInteract(PlayerInteractEvent.RightClickBlock event) {
        Player player = event.getEntity();
        ItemStack mainHand = player.getMainHandItem();
        if (mainHand.getItem() instanceof ItemSlashBlade){
            // 服务端逻辑：检查按键状态并取消事件
            if (!player.level().isClientSide()) {
                boolean isCtrlDown = player.getPersistentData().getBoolean("sua_ctrl_down");
                if (Config.disableOtherInteractions.get() && !isCtrlDown) {
                    event.setCanceled(true);
                }
            } 
            // 客户端逻辑：仅显示提示消息
            else {
                if (Config.disableOtherInteractions.get() && !SUAModKeyMappings.TypeMaker.isDown()) {
                    player.displayClientMessage(Component.translatable("warning.sua.nodois",
                            SUAModKeyMappings.TypeMaker.getKey().getDisplayName()), true);
                }
            }
        }
    }
    
    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public static void onEntityInteract(PlayerInteractEvent.EntityInteract event) {
        Player player = event.getEntity();
        ItemStack mainHand = player.getMainHandItem();
        if (mainHand.getItem() instanceof ItemSlashBlade){
            if (event.getTarget() instanceof BladeStandEntity) return;
            
            // 服务端逻辑：检查按键状态并取消事件
            if (!player.level().isClientSide()) {
                boolean isCtrlDown = player.getPersistentData().getBoolean("sua_ctrl_down");
                if (Config.disableOtherInteractions.get() && !isCtrlDown) {
                    event.setCanceled(true);
                }
            } 
            // 客户端逻辑：仅显示提示消息
            else {
                if (Config.disableOtherInteractions.get() && !SUAModKeyMappings.TypeMaker.isDown()) {
                    player.displayClientMessage(Component.translatable("warning.sua.nodois", 
                            SUAModKeyMappings.TypeMaker.getKey().getDisplayName()), true);
                }
            }
        }
    }
}